/**
 * zmn公共-查询选择渠道
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/05/21 17:04
 */
layui.define(['index', 'form', 'table', 'laypage','zmnSelect'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let zmnSelect = layui.zmnSelect;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-104'
    });
    laypage.render(zmnPage.laypageOptions(json));

    // 下拉框
    zmnSelect.selectPlat('plat', {
        selectedValues: json.plat,
        isRadio: true,
        events: {
            changed: function (values) {}
        }
    });

    // 获取弹出窗口索引
    var index = parent.layer.getFrameIndex(window.name);

    /**
     * 回调函数方法
     * @param callbackUrl
     * @param params
     */
    var callback = function(callbackUrl, params) {
        $.ajax({
            async: false,
            type: "POST", url: callbackUrl,
            dataType: "json", data: params,
            success:function(json) {
                parent.layer.msg(json.message);
                if(json.status == zmnStatus.SUCCESS) {
                    setTimeout(function(){
                        parent.layer.close(index)
                        window.parent.location.reload()
                    }, 1000);
                }
                return false;
            },
            error:function(XMLHttpRequest, tcategoryStatus, errorThrown){}
        });
        return false;
    }

    // 确认选中（批量）
    table.on("toolbar(zmn-table)", function (obj) {
        if(obj.event == "addBatch") {
            if(zmnUtil.isEmptyNull(json.callbackUrl)) {
                return false;
            }
            var channelIds = zmnPage.layTabldIds(obj, "channelId");
            if(channelIds.length === 0) {
                return parent.layer.alert("请选择或指定渠道");
            }

            var params = { "keyId": json.keyId, "channelIds": channelIds.join(",") }
            callback(json.callbackUrl, params);
            return false;
        }
    });
    // 选中（单个）
    table.on('tool(zmn-table)', function(obj){
        var channelId = obj.data.channelId;
        if(obj.event === 'add'){
            if(zmnUtil.isEmptyNull(json.callbackUrl)) {
                return false;
            }
            if(!zmnUtil.isNotZeroInteger(channelId)) {
                return parent.layer.alert("请选择或指定渠道");
            }
            var params = { "keyId": json.keyId, "channelIds": channelId }
            callback(json.callbackUrl, params);
            return false;
        }
    });

    layer.ready(function(){
        if(json.status){
            $("#status").val(json.status);
            form.render();
        }
    });

    //对外暴露的接口
    exports('common_channel_list', {});
});
